﻿using UnityEngine;
using UFramework.Tools;
using UnityEngine.UI;
using System;

public abstract class Form
{
    protected abstract string AssetName { get; }
    private GameObject gameObject;
    public GameObject GameObject => gameObject;
    public Transform Transform => gameObject.transform;
    public string Name { get; }

    //只能用于单例
    protected Form()
    {
    }
    //用于多窗口
    public Form(string name)
    {
        this.Name = name;
    }
    protected State state = State.off;
    public virtual void LoadAsset()
    {
        if (!gameObject)
        {
            var prefab = Resources.Load<GameObject>(AssetName);
            gameObject = UnityEngine.Object.Instantiate<GameObject>(prefab);//实例化窗口物体
            gameObject.transform.SetParent(UIManager.Instance.transform, false);
        }
    }

    //初始化组件,加载游戏物体,绑定UI交互事件
    protected virtual void IniComponent()
    {
        if (!gameObject)
        {
            LoadAsset();
        }
        //找到所有的子物体,绑定点击事件...子类完成
        TransformHelper.ForEveryChild(gameObject.transform, ForEveryChild);
    }
    private void ForEveryChild(Transform tf)
    {
        if (tf.name == "Button_Close")
        {
            tf.GetComponent<Button>().onClick.AddListener(() => { Close(); });
        }

        ForEveryUI(tf);
    }

    public virtual void Show()
    {
        if (!gameObject)
        {
            IniComponent();
        }

        switch (state)
        {
            case State.on:
                break;
            case State.off:
                gameObject.SetActive(true);//打开物体
                if (state == State.off)
                {
                    //TODO: 监听数据模块的变化
                    OnShow();//主要用于第一次显示数据
                    AddDataEventListener();//在UI的打开过程中实时刷新
                }
                break;
            case State.hide:
                gameObject.SetActive(true);
                OnShow();
                break;
        }
        state = State.on;
        UIManager.Instance.Top(this);//放到最顶层
    }

    //单纯的隐藏游戏物体,逻辑正常执行
    public void Hide()
    {
        gameObject.SetActive(false);
        state = State.hide;
    }
    //隐藏游戏物体,取消数据监听
    public virtual void Close()
    {
        if (state == State.off) return;
        gameObject.SetActive(false);
        //TODO 取消数据监听
        RemoveDataEventListener();
        OnClose();
        state = State.off;
    }

    public void Close(bool destroyGameObject)
    {
        Close();
        UnityEngine.Object.Destroy(gameObject);
    }
    protected void AddButtonListener(Transform tf, UnityEngine.Events.UnityAction action)
    {
        tf.GetComponent<Button>().onClick.AddListener(action);
    }

    #region 抽象方法
    /// <summary>
    /// 绑定每一个UI的事件,你需要判断每一个Transform参数的名字,获取具体的UI组件绑定事件
    /// </summary>
    /// <param name="tf">页面上的每一个UI物体</param>
    protected abstract void ForEveryUI(Transform tf);
    /// <summary>
    /// 添加数据监听
    /// </summary>
    protected abstract void AddDataEventListener();
    /// <summary>
    /// 移除数据监听
    /// </summary>
    protected abstract void RemoveDataEventListener();
    #endregion 抽象方法

    protected virtual void OnShow()
    {
    }

    protected virtual void OnClose()
    {
    }

    protected enum State
    {
        on,
        off,
        hide,
    }
}